package t327.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import t327.mapper.*;
import t327.pojo.*;
import t327.pojo.views.ContractOrderClientView;
import t327.pojo.views.ProductionPlanOrderSizeDepartmentView;
import t327.service.iter.IContractService;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

@Service
@Transactional
public class ContractServiceImpl implements IContractService {
    @Autowired
    private ProductionPlanOrderSizeDepartmentMapper productionPlanOrderSizeDepartmentMapper;
    @Autowired
    private ContractMapper contractMapper;
    @Autowired
    private ColorMaterialsRelMapper colorMaterialsRelMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private ProductionPlanmapper productionPlanMapper;
    @Autowired
    private ContractOrderClientMapper contractOrderClientMapper;
    @Autowired
    private SubmissionMapper submissionMapper;
  /*  @Autowired
    private OrderStyleRelMapper orderStyleRelMapper;*/
    @Autowired
    private ProductionPlanmapper productionPlanmapper;
    @Override
    public List<Contract> queryContract() {
        return contractMapper.queryAll();
    }

    @Override
    public List<OrderForm> queryOrder() {
        return contractMapper.queryOrder();
    }

    @Override
    public int addContract(Contract contract) {
        return   contractMapper.addContract(contract);

    }

    @Override
    public List<ContractOrderClientView> quertyContractOrderClient() {
        return contractOrderClientMapper.quertyContractOrderClient();
    }

    @Override
    public  List<ColorMaterialsRel> queryColorMaterials() {
        return colorMaterialsRelMapper.queryColorMaterials();
    }

    @Override
    public List<ProductionPlan> queryProduction() {
        return productionPlanMapper.queryAll();
    }

    @Override
    public ProductionPlan queryProductionPlan(Long productionPlanId) {
        return productionPlanmapper.queryProductionPlan(productionPlanId);
    }

    @Override
    public List<Department> queryDepartment()  {
        return departmentMapper.queryDepartment();
    }

    @Override
    public HashMap<String,String> addSubmission(Submission submission) {
        HashMap<String,String> map = new HashMap<>();
        Long productionPlanId = submission.getProductionPlanId();
        ProductionPlan productionPlan1 = productionPlanmapper.queryProductionPlan(productionPlanId);
        Long orderId = productionPlan1.getOrderId();
        Date scheduleTime = productionPlan1.getScheduleTime();
        String dateStr=scheduleTime.toString();
        Long schedeleCount = productionPlan1.getSchedeleCount();
        Long ocsrId = submission.getOrderId();
        SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd 00:00:00 zzz yyyy", Locale.ENGLISH);
        String formattedDate = dateFormat.format(new Date());
        Long producedCount = submission.getProducedCount();
        if (!dateStr.equals(formattedDate)){
            map.put("code","202");
            map.put("msg","请选择当天的生产计划");
        }else {
            if (orderId!=ocsrId){
                map.put("code","201");
                map.put("msg","提交的订单编号不正确，请重新选择");
            }else {
                if (producedCount*1.02>schedeleCount && producedCount*1.02<=schedeleCount){
                    map.put("code","203");
                    map.put("msg","提交的数量已超出当前计划的产量，请立即跟换其他款号或订单的产品");
                }else {
                    int i = submissionMapper.addSubmission(submission);
                    if (i>0){
                        map.put("code","200");
                        map.put("msg","提交成功");
                    }
                }
            }
        }
        return map;
    }

    @Override
    public List<ProductionPlanOrderSizeDepartmentView> queryAll() {
        return productionPlanOrderSizeDepartmentMapper.queryAll();
    }

    @Override
    public int updateContract(String contractNumber) {
        int i = contractMapper.updateContract(contractNumber);
        return i;
    }

    @Override
    public int updateContractPay(Long contractId) {
        return contractMapper.updateContractPay(contractId);
    }

   /* public static void main(String[] args) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy  00:00:00 zz", Locale.ENGLISH);
        String formattedDate = dateFormat.format(new Date());
        System.out.println("Current date: " + formattedDate);
    }*/
}
